﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;
namespace Billing.Transactions
{
    public partial class PaymentTransferForm : Billing.Transactions.TransactionBase
    {
        public PaymentTransferForm()
        {
            InitializeComponent();
        }

        AccountsView ClientFrom = null;
        AccountsView ClientTo = null;

        bool clientTypesAreDifferent;

        private void ShowHideDestinationAmount()
        {
            if (ClientFrom == null || ClientTo == null)
                clientTypesAreDifferent = false;
            else 
                clientTypesAreDifferent = ClientFrom.Client.ClientTypeID != ClientTo.Client.ClientTypeID;

            txtAmount.ReadOnly = !clientTypesAreDifferent;
        }

        private void trvClientFrom_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.AccountsSelectionForm frm = new Billing.Win.AccountsSelectionForm(false, true);

            frm.AllowSelect = true;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientFrom = (AccountsView)frm.SelectedObjects[0].CurrentObject;
                this.trvClientFrom.BaseObject = ClientFrom;
                //ShowHideDestinationAmount();
            }
        }

        private void trvClientTo_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.AccountsSelectionForm frm = new Billing.Win.AccountsSelectionForm(false, true);
            frm.AllowSelect = true;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientTo = (AccountsView)frm.SelectedObjects[0].CurrentObject;
                this.trvClientTo.BaseObject = ClientTo;
              //  ShowHideDestinationAmount();
            }
        }

        public override void OnSave()
        {
            try
            {
                decimal Amount;
                Amount =  Convert.ToDecimal(this.txtAmount.Value);
                if (Amount == 0)
                {
                    this.ShowWarning("ნულოვან თანხაზე გატარება დაუშვებელია!");
                    return;
                }

                if (this.ClientFrom.ID == this.ClientTo.ID)
                {
                    this.ShowWarning("ერთი და იგივე ანგარიშზე თანხის გადატანა დაუშვებელია!");
                    return;
                }
                this.CurrentHeader.HeaderTypeID = 10;
                this.CurrentHeader.Description = "Payment Transfer: " + this.txtDescription.Text;
               
                this.CurrentHeader.PaymentPointID = 15;

                this.AddNewLine();
                this.CurrentLine.AccountID = ClientFrom.ID;

                this.CurrentLine.TaxID = ClientFrom.Client.TaxID;
                this.CurrentLine.Amount = -Amount;

                this.AddNewLine();
                this.CurrentLine.AccountID = ClientTo.ID;
                this.CurrentLine.TaxID = ClientTo.Client.TaxID;
                this.CurrentLine.Amount = Amount;

                base.OnSave();
            }
            catch (Exception ex)
            {
                this.ShowError(ex);
            }
            this.Close();
        }

        private void txtAmount_Enter(object sender, EventArgs e)
        {
            this.txtAmount.SelectAll();
        }
    }
}
